package com.xxd.rpt3.controller;

import com.xxd.rpt3.Entity.User;
import com.xxd.rpt3.service.UserService;
import com.xxd.rpt3.vo.ResultModel;
import com.xxd.utils.JwtUtil;
import com.xxd.utils.PasswordUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("public")
public class PublicController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "login", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password) {
		try {
			User user = userService.findByName(username);
			if (user == null) {
				return new ResultModel(HttpStatus.UNAUTHORIZED, "用户名不存在");
			}

			Map<String, String> map = new HashMap<>();
			if (PasswordUtil.verify(user, password)) {
				String newToken = userService.signToken(user);
				map.put(JwtUtil.TOKEN_NAME, newToken);
				return new ResultModel(map);
			}else{
				return new ResultModel(HttpStatus.UNAUTHORIZED, "用户名或密码错误");
			}
		} catch (Exception e) {
			return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
		}
	}

	@RequestMapping(value = "logout", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel logout(HttpServletRequest httpRequest) {
		try {
			String token = (String) httpRequest.getAttribute(JwtUtil.TOKEN_NAME);
			userService.logout(token);
			return new ResultModel();
		} catch (Exception e) {
			return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
		}
	}

	@RequestMapping(value = "register", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel register(String username, String password) {
		try {
			User user = userService.findByName(username);
			if (user != null) {
				return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR, "用户名已存在");
			} else {
				user = new User();
			}
			user.setCode(username);
			user.setPassword(password);
			userService.saveUser(user);
			return new ResultModel(true);
		} catch (Exception e) {
			return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
		}
	}

	@RequestMapping(value = "tokenError", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel tokenError() {
		return new ResultModel(HttpStatus.UNAUTHORIZED, "凭证无效");
	}


	@RequestMapping(value = "createShareToken", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel createShareToken(){
		User user = new User();
		user.setCode("share-"+PasswordUtil.createSalt());
		user.setSalt(PasswordUtil.createSalt());
		user.setPassword(PasswordUtil.encryption("1", user.getSalt()));
		String newToken = userService.signToken(user,1);
		return new ResultModel("http://localhost:8080/testSwagger/test1?token="+newToken);
	}
}
